package com.visikard.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnDrawListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.edmodo.cropper.CropImageView;
import com.visikard.application.VKRegApplication;
import com.visikard.ui.R;
import com.visikard.utility.ScalingUtilities;
import com.visikard.utility.ScalingUtilities.ScalingLogic;

public class CouponCameraPreviewActivity extends BaseFragmentActivity  {

	private CropImageView mImageView;
	private Button mButton_save, mButton_cancel;

	public static final String CROPPED_BITMAP = "cropped_bitmap";

	private static final String ASPECT_RATIO_X = "ASPECT_RATIO_X";
	private static final String ASPECT_RATIO_Y = "ASPECT_RATIO_Y";

	private int mAspectRatioX = 100;
	private int mAspectRatioY = 50;
	
	
	private void initControls() {
		mImageView = (CropImageView) findViewById(R.id.imageView1);
		mImageView.setGuidelines(0);
		mImageView.setFixedAspectRatio(true);
		mImageView.setAspectRatio(mAspectRatioX, mAspectRatioY);
		mButton_save = (Button) findViewById(R.id.btn_save);
		mButton_cancel = (Button) findViewById(R.id.btn_cancel);
		mButton_save.setOnClickListener(new MyOnClickListener());
		mButton_cancel.setOnClickListener(new MyOnClickListener());

	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.coupon_camera_preview);
		initControls();
		Bundle bundle = this.getIntent().getExtras();
		String picturePath = bundle.getString("path");
		int[] dimen = VKRegApplication.instance
				.getDimensionDevice(CouponCameraPreviewActivity.this);

		float density=VKRegApplication.instance.getDensity();
		cropButtonPressed(picturePath, dimen[0],
				(int) (dimen[1]-200*density));
		

	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v == mButton_save) {
				Bitmap croppedImage = mImageView.getCroppedImage();
				VKRegApplication.instance.putData(CROPPED_BITMAP, croppedImage);
				Intent intent = new Intent(CouponCameraPreviewActivity.this,
						CouponInformationActivity.class);
				startActivity(intent);
			} else if (v == mButton_cancel) {
				setResult(RESULT_CANCELED);
				finish();
			}
		}

	}

	protected void cropButtonPressed(String path, int mDstWidth, int mDstHeight) {

		Bitmap unscaledBitmap = ScalingUtilities.decodeResource(path,
				mDstWidth, mDstHeight, ScalingLogic.CROP);

		// Part 2: Scale image
		Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(
				unscaledBitmap, mDstWidth, mDstHeight, ScalingLogic.CROP);
		unscaledBitmap.recycle();

		mImageView.setImageBitmap(scaledBitmap);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);

		outState.putInt(ASPECT_RATIO_X, mAspectRatioY);
		outState.putInt(ASPECT_RATIO_Y, mAspectRatioY);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		mAspectRatioX = savedInstanceState.getInt(ASPECT_RATIO_X);
		mAspectRatioY = savedInstanceState.getInt(ASPECT_RATIO_Y);
	}

}
